package com.gklyj.controller.administer;


import com.alibaba.fastjson.JSONObject;
import com.gklyj.pojo.User;
import com.gklyj.pojo.Zhanghao;
import com.gklyj.pojo.user_zhanghao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@Transactional
public class Qz_userController {

    @Autowired
    private com.gklyj.service.zhanghaoService zhanghaoService;

    @Autowired
    private com.gklyj.service.userService userService;

    @Autowired
    private com.gklyj.service.user_zhanghaoService user_zhanghaoService;


    /*
    用于管理员后台跳转求职者信息管理页面
     */
    @GetMapping("/toqz_user")
    public ModelAndView toQz_User(){
        ModelAndView modelAndView = new ModelAndView("administer/qz-user");
        return modelAndView;
    }

    /*
    用于求职者信息管理页面，查找所有求职者信息并返回给  求职者信息管理页面
     */
    @GetMapping("/findList_Qz")
    @ResponseBody
    public List<User> findList_User(){
        List<User> userList = userService.findUserByRole("student");
        return userList;
    }

    /*
       根据前端传入数据进行搜索
        */
    @PostMapping("/qz_search")
    @ResponseBody
    public List<User> searchQz_user(@RequestBody User user){
        //System.out.println(user);
        List<User> QzuserList = userService.searchQzUser(user);
        System.out.println(QzuserList);
        return QzuserList;
    }

    /*
    跳转编辑页面
     */
    @GetMapping("/toqzEdit")
    public String qzEditPage(@RequestParam("id") String id, Model model){
        System.out.println("========================================================="+id);
        User user = userService.findUserById(Integer.valueOf(id));
        //ModelAndView modelAndView = new ModelAndView("administer/table/edit");
        model.addAttribute("user1",user);
        //modelAndView.addObject("obj",user);
        return "administer/table/edit";
    }

    /*
       修改求职者信息  招聘单位信息共用
    */
    @PostMapping("/editQzData")
    @ResponseBody
    public Boolean editQz(@RequestBody User user){
        boolean result = userService.updateById(user);
        if (result){
            return true;
        }else {
            return false;
        }
    }

    /*
       删除求职者信息  招聘单位信息共用
    */
    @PostMapping("/deleteQzData")
    @ResponseBody
    public Boolean deleteQz(@RequestBody User user){
        //删除user表相应信息
        Boolean result = userService.removeById(user.getU_id());
        Integer z_id = user_zhanghaoService.findZ_id(user.getU_id());
        //删除user_zhanghao表相应信息
        Boolean result1 = user_zhanghaoService.deleteByU_id(user.getU_id());
        //删除zhanghao表相应信息
        Boolean result2 = zhanghaoService.removeById(z_id);
//        System.out.println(123);
//        Boolean result = true;
        if (result && result1 && result2){
            return true;
        }else {
            return false;
        }
    }

    /*
       删除多个求职者信息   删除多个招聘单位信息也是这里
    */
    @PostMapping("/deleteQzDataList")
    @ResponseBody
    public Boolean deleteQzDataList(@RequestBody List<User> user){
        //统计user条数
        Integer sum = user.size();
        sum=sum-1;
        //批量删除，将id保存 && 批量查询z_id
        ArrayList<Integer> arrayList=new ArrayList();
        ArrayList<Integer> list_z_id=new ArrayList();
        while (sum>=0){
            arrayList.add(user.get(sum).getU_id());
            sum--;
        }
        sum = user.size();
        sum=sum-1;
        //用于查找每个u_id对应的z_id
        List<user_zhanghao> user_zhanghaos = user_zhanghaoService.listByIds(arrayList);
        while (sum>=0){
            //将查询的z_id逐个保存
            list_z_id.add(user_zhanghaos.get(sum).getZ_id());
            sum--;
        }
        Boolean result = userService.removeByIds(arrayList);
        Boolean result1 = user_zhanghaoService.removeByIds(arrayList);
        Boolean result2 = zhanghaoService.removeByIds(list_z_id);
        //Boolean result = true;
        if (result && result1 && result2){
            return true;
        }else {
            return false;
        }
    }

    /*
    跳转添加信息页面
     */
    @GetMapping("/toqzAdd")
    public String toQzAddPage(){
        return "administer/table/add";
    }

    /*
    添加求职者信息
     */
    @PostMapping("/addQzUser")
    @ResponseBody
    public Boolean addQz(@RequestBody String data){
        //将数据转化为json格式
        JSONObject json = JSONObject.parseObject(data);
        User user = new User();
        Zhanghao zhanghao = new Zhanghao();
        user_zhanghao userZhanghao = new user_zhanghao();
        //user信息
        user.setName((String) json.get("name"));
        user.setAge(json.getInteger("age"));
        user.setSex((String) json.get("sex"));
        user.setGrade((String) json.get("grade"));
        user.setMajor((String) json.get("major"));
        user.setPhone_number((String) json.get("phone_number"));
        user.setStu_id((String) json.get("stu_id"));
        boolean r = userService.save(user);

        //zhanghao信息
        zhanghao.setUsername((String) json.get("username"));
        zhanghao.setPassword((String) json.get("password"));
        zhanghao.setRole("student");
        boolean r1 = zhanghaoService.save(zhanghao);

        int u_id = userService.findU_id((String) json.get("name"),(String) json.get("phone_number"));
        int z_id = zhanghaoService.findZ_id((String) json.get("username"),(String) json.get("password"));
        userZhanghao.setU_id(u_id);
        userZhanghao.setZ_id(z_id);
        boolean r2 = user_zhanghaoService.save(userZhanghao);
        if (r && r1 && r2){
            return true;
        }else {
            return false;
        }

    }
}
